package com.wheat.vipcard.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;
import com.wheat.vipcard.bean.Cardtype;
import com.wheat.vipcard.bean.CardtypeDAO;
import com.wheat.vipcard.utils.PageResultSet;
import com.wheat.vipcard.utils.PageUtil;

public class CardTypeAction extends ActionSupport {

	private HttpServletRequest request;
	/**
	 * 
	 */
	private static final long serialVersionUID = 12L;

	private Cardtype cardtype;

	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Cardtype getCardtype() {
		return cardtype;
	}

	public void setCardtype(Cardtype cardtype) {
		this.cardtype = cardtype;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveCardType() throws Exception {
		Transaction tr = null;
		try {
			if (cardtype != null) {
				CardtypeDAO dao = new CardtypeDAO();
				tr = dao.getSession().beginTransaction();
				dao.save(cardtype);
				tr.commit();
				dao.getSession().close();
			}
		} catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		}
		return "saveCardType";
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listCardTyeByPage() throws Exception {
		request = ServletActionContext.getRequest();
		CardtypeDAO typeDao = new CardtypeDAO();
		try {
			int sPage = Integer.valueOf((String) request.getParameter("page"));
			PageResultSet<Cardtype> pageResultSetList = typeDao.findCardTypeByPage(cardtype, sPage,
					new PageUtil().getPageSize());
			Map<String, Object> resultJsonMap = new HashMap<String, Object>();
			resultJsonMap.put("event", pageResultSetList.getList());
			resultJsonMap.put("pageBean", pageResultSetList.getPage());
			resultJsonMap.put("pageCount", pageResultSetList.getPage().getTotalPage());
			this.result = JSONObject.fromObject(resultJsonMap).toString();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}

}
